package vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization;

import java.util.Observer;

import javax.swing.JMenuItem;

import vg.services.graph_view_manager.interfaces.graph_view.IGraphObservable;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVGraphView4Patching;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVPatchManager;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.patches.DefaultPatch;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.shared.Utils;

/**
 * Implementation of IGraphObservable.
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class SGVGraphObservable implements IGraphObservable {
	// Main components
	private ISGVPatchManager sgv_patch_manager;

	public SGVGraphObservable(ISGVPatchManager sgv_patch_manager) {
		this.sgv_patch_manager = sgv_patch_manager;
	}
	
	@Override
	public void addPopupMenuItem(final JMenuItem item) {
		DefaultPatch patch = new DefaultPatch("addPopupMenuItem") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.addPopupMenuItem(item);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);
	}
	
	@Override
	public void removePopupMenuItem(final JMenuItem item) {
		DefaultPatch patch = new DefaultPatch("removePopupMenuItem") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.removePopupMenuItem(item);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
	}
	
	@Override
	public void addObserver(final Observer o) {
		DefaultPatch patch = new DefaultPatch("addObserver") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.addObserver(o);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
	}
	
	@Override
	public void deleteAllObservers() {
		DefaultPatch patch = new DefaultPatch("deleteAllObservers") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.removeAllObservers();
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);		
	}
	
	@Override
	public void removeObserver(final Observer o) {
		DefaultPatch patch = new DefaultPatch("removeObserver") {
			@Override
			public void apply(ISGVGraphView4Patching graphView4patching) {
				graphView4patching.removeObserver(o);
			}
		};
		
		Utils.add_patch2patch_manager_and_wait_result(sgv_patch_manager, patch);			
	}
}
